package com.qpx.jxcbiz.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qpx.jxc.common.dto.GoodDto;
import com.qpx.jxc.common.utils.ExcelBean;
import com.qpx.jxc.common.utils.ExcelUtils;
import com.qpx.jxcbiz.entity.TGoods;
import com.qpx.jxcbiz.mapper.TGoodsMapper;
import com.qpx.jxcbiz.service.ITGoodsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.beans.IntrospectionException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mht
 * @since 2020-05-22
 */
@Service
public class TGoodsServiceImpl extends ServiceImpl<TGoodsMapper, TGoods> implements ITGoodsService {


    @Override
    public XSSFWorkbook downLoadGoodsExcel(GoodDto goodDto) throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {

        PageHelper.startPage(goodDto.getPageNo(),goodDto.getPageSize());
        Wrapper wrapper = new EntityWrapper();
        if (StringUtils.isNotBlank(goodDto.getName())){
            wrapper.like("name",goodDto.getName());
        }
        List<TGoods> tGoods = this.selectList(wrapper);
        PageInfo<TGoods> pageInfo = new PageInfo<>(tGoods);
        Map<Integer, List<ExcelBean>> map = new HashMap<Integer, List<ExcelBean>>();

        List<ExcelBean> excelBeans = new ArrayList<>();
        excelBeans.add(new ExcelBean("id","id",0));
        excelBeans.add(new ExcelBean("商品名称","name",0));
        excelBeans.add(new ExcelBean("生产公司","producer",0));
        excelBeans.add(new ExcelBean("库存量","inventoryQuantity",0));
        excelBeans.add(new ExcelBean("采购价格","purchasingPrice",0));
        excelBeans.add(new ExcelBean("销售价格","sellingPrice",0));
        excelBeans.add(new ExcelBean("单元","unit",0));
        excelBeans.add(new ExcelBean("最后购买架格","lastPurchasingPrice",0));
        //excelBeans.add(new ExcelBean("状态","stateStr",0));
        excelBeans.add(new ExcelBean("款式","model",0));
        map.put(0,excelBeans);
        XSSFWorkbook book = ExcelUtils.createExcelFile(TGoods.class, pageInfo.getList(), map, "商品列表");
        return book;
    }

    @Override
    public void uploadExcel(InputStream inputStream, String getOriginalFilename) throws Exception {
        List<List<Object>> bankListByExcel = ExcelUtils.getBankListByExcel(inputStream, getOriginalFilename);
        List<TGoods> goodsList = new ArrayList<>();
        for (List<Object> objects : bankListByExcel) {
            TGoods tGoods = new TGoods();
            tGoods.setName(String.valueOf(objects.get(1)));
            tGoods.setProducer(String.valueOf(objects.get(2)));
            tGoods.setInventoryQuantity(Integer.valueOf(objects.get(3).toString()));
            tGoods.setPurchasingPrice(Float.valueOf(objects.get(4).toString()));
            tGoods.setSellingPrice(Float.valueOf(objects.get(5).toString()));
            tGoods.setUnit(objects.get(6).toString());
            tGoods.setLastPurchasingPrice(Float.valueOf(objects.get(7).toString()));
            tGoods.setState(2);
            tGoods.setModel(objects.get(8).toString());
            tGoods.setMinNum(100);
            goodsList.add(tGoods);
        }
        this.insertBatch(goodsList);
    }
}
